package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.rebate.ChinaUserRebate;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChinaUserRebateDaoImpl extends TempDataObjectDao<ChinaUserRebate> {

    public ChinaUserRebate getServerChinaUserRebate(String openId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_rebate_cn where openId = ? and isRebate = 0";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setString(i++, openId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChinaUserRebate data = parseServerData(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public boolean updateServerChinaUserRebate(String openId) {
        PreparedStatement call = null;
        String sql = "UPDATE t_s_rebate_cn SET isRebate=? where openId=?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setBoolean(i++, true);
            call.setString(i++, openId);
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    public Map<String, ChinaUserRebate> getServerChinaUserRebateMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, ChinaUserRebate> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_s_rebate_cn";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ChinaUserRebate data = parseServerData(rs);
                dataMap.put(data.getOpenId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<String, ChinaUserRebate> getChinaUserRebateMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, ChinaUserRebate> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_rebate";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ChinaUserRebate data = parse(rs);
                dataMap.put(data.getOpenId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(ChinaUserRebate data) {
        boolean result = false;
        getDb().setConnName(DbNameStrategy.getUserDb());
        String sql = "INSERT INTO t_u_rebate" +
                " (userId,openId,platformUserId,vipExp,buySecret,buyFund,buyYear,buyMonth,isGetVipExp,isGetSecret,isGetFund,isGetYear,isGetMonth,rebateUserId,rebateUserName) " +
                " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        Map<Integer, DbParameter> para = new HashMap<>();
        int i = 1;
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getOpenId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getPlatformUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getVipExp()));
        para.put(i++, new DbParameter(Types.INTEGER, data.isBuySecret()));
        para.put(i++, new DbParameter(Types.INTEGER, data.isBuyFund()));
        para.put(i++, new DbParameter(Types.INTEGER, data.isBuyYear()));
        para.put(i++, new DbParameter(Types.INTEGER, data.isBuyMonth()));
        para.put(i++, new DbParameter(Types.INTEGER, data.isGetVipExp()));
        para.put(i++, new DbParameter(Types.INTEGER, data.isGetSecret()));
        para.put(i++, new DbParameter(Types.INTEGER, data.isGetFund()));
        para.put(i++, new DbParameter(Types.INTEGER, data.isGetYear()));
        para.put(i++, new DbParameter(Types.INTEGER, data.isGetMonth()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRebateUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getRebateUserName()));
        mapList.add(para);
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    @Override
    protected boolean updateData(ChinaUserRebate data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_rebate SET isGetVipExp=?,isGetSecret=?,isGetFund=?,isGetYear=?,isGetMonth=?,updateTime=?,rebateUserId=?,rebateUserName=? where openId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setBoolean(i++, data.isGetVipExp());
            call.setBoolean(i++, data.isGetSecret());
            call.setBoolean(i++, data.isGetFund());
            call.setBoolean(i++, data.isGetYear());
            call.setBoolean(i++, data.isGetMonth());
            call.setLong(i++, data.getUpdateTime());
            call.setLong(i++, data.getRebateUserId());
            call.setString(i++, data.getRebateUserName());
            call.setString(i++, data.getOpenId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected ChinaUserRebate parseData(ResultSet rs) throws SQLException {
        ChinaUserRebate data = new ChinaUserRebate();
        data.setUserId(rs.getLong("userId"));
        data.setOpenId(rs.getString("openId"));
        data.setPlatformUserId(rs.getString("platformUserId"));
        data.setVipExp(rs.getLong("vipExp"));
        data.setBuySecret(rs.getBoolean("buySecret"));
        data.setBuyFund(rs.getBoolean("buyFund"));
        data.setBuyYear(rs.getBoolean("buyYear"));
        data.setBuyMonth(rs.getBoolean("buyMonth"));

        data.setGetVipExp(rs.getBoolean("isGetVipExp"));
        data.setGetSecret(rs.getBoolean("isGetSecret"));
        data.setGetYear(rs.getBoolean("isGetYear"));
        data.setGetMonth(rs.getBoolean("isGetMonth"));

        data.setUpdateTime(rs.getLong("updateTime"));

        data.setRebateUserId(rs.getLong("rebateUserId"));
        data.setRebateUserName(rs.getString("rebateUserName"));
        return data;
    }

    protected ChinaUserRebate parseServerData(ResultSet rs) throws SQLException {
        ChinaUserRebate data = new ChinaUserRebate();
        data.setUserId(StringUtils.stringToLongList(rs.getString("userId"),",").get(0));
        data.setOpenId(rs.getString("openId"));
        data.setPlatformUserId(rs.getString("platformUserId"));
        data.setVipExp(rs.getLong("vipExp"));
        data.setBuySecret(rs.getBoolean("buySecret"));
        data.setBuyFund(rs.getBoolean("buyFund"));
        data.setBuyYear(rs.getBoolean("buyYear"));
        data.setBuyMonth(rs.getBoolean("buyMonth"));

        data.setGetVipExp(false);
        data.setGetSecret(false);
        data.setGetYear(false);
        data.setGetMonth(false);
        data.setUpdateTime(0);
        data.setRebateUserId(0);
        data.setRebateUserName("");
        data.setInsertOption();
        return data;
    }
}
